package edu.nuc.ahm.juc.unsafecollection;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/*
* 同理可证： ConcurrentModificationException
*
* HashSet 的底层  ：new HashMap<>() 你把值作为key 不就行了
*      public boolean add(E e){
*          return map.put(e,PRESENT)  PRESENT = new Object() 不变的值
*      }
*
*
* */
public class SetTest {
    public static void main(String[] args) {
//        HashSet<String> set = new HashSet<>();
//      解决方案一：Collections.synchronizedSet(new HashSet<>)
//        Set<String> set = Collections.synchronizedSet(new HashSet<>());
//      解决方案二：CopyOnWrite
        Set<String> set = new  CopyOnWriteArraySet();
        for (int i = 1; i <=10; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,10));
                System.out.println(set);
            },String.valueOf(i)).start();
        }

    }





}
